import Settings from '../config/Settings.js';

export default class Powerup {
  constructor(game, type, x, y) {
    this.game = game;
    this.type = type;
    this.x = x;
    this.y = y;
    
    const config = Settings.powerUps.types[type];
    this.width = config.sprite.width;
    this.height = config.sprite.height;
    this.color = config.sprite.color;
    this.symbol = config.symbol;
    
    this.floatOffset = 0;
    this.floatSpeed = 0.05;
    this.rotationAngle = 0;
  }
  
  update() {
    // 漂浮动画
    this.floatOffset = Math.sin(this.game.gameTime * this.floatSpeed) * 10;
    this.rotationAngle += 0.02;
    
    // 检测与玩家的碰撞
    if (this.checkCollision(this.game.player)) {
      this.applyEffect();
      // 生成收集特效
      this.createCollectEffect();
      return true; // 标记为需要移除
    }
    
    return false;
  }
  
  render(ctx) {
    ctx.save();
    ctx.translate(this.x, this.y + this.floatOffset);
    ctx.rotate(this.rotationAngle);
    
    // 绘制光环效果
    const gradient = ctx.createRadialGradient(0, 0, 0, 0, 0, this.width);
    gradient.addColorStop(0, this.color);
    gradient.addColorStop(1, 'rgba(255,255,255,0)');
    ctx.fillStyle = gradient;
    ctx.globalAlpha = 0.3;
    ctx.fillRect(-this.width, -this.height, this.width * 2, this.height * 2);
    
    // 绘制道具主体
    ctx.globalAlpha = 0.8;
    ctx.fillStyle = this.color;
    ctx.fillRect(-this.width/2, -this.height/2, this.width, this.height);
    
    // 绘制道具图标
    ctx.fillStyle = '#FFFFFF';
    ctx.font = '20px Arial';
    ctx.textAlign = 'center';
    ctx.textBaseline = 'middle';
    ctx.fillText(this.symbol, 0, 0);
    
    ctx.restore();
  }
  
  createCollectEffect() {
    // 创建粒子效果
    this.game.particleSystem.createParticles(
      this.x,
      this.y,
      this.color,
      15, // 粒子数量
      5,  // 速度
      5,  // 大小
      30  // 生命周期
    );
  }
  
  applyEffect() {
    const config = Settings.powerUps.types[this.type];
    const player = this.game.player;
    const duration = config.duration;

    switch(this.type) {
      case 'giant':
        // 变大效果
        const originalSize = {
          width: player.width,
          height: player.height
        };
        
        // 增大体型
        player.width *= config.effects.sizeMultiplier;
        player.height *= config.effects.sizeMultiplier;
        
        // 增加伤害和跳跃力
        player.damageMultiplier = config.effects.damageMultiplier;
        player.jumpForceMultiplier = config.effects.jumpForceMultiplier;
        
        // 添加视觉效果
        this.game.particleSystem.createParticles(
          player.x,
          player.y,
          config.sprite.color,
          20,
          6,
          8,
          40
        );
        
        // 设置定时器恢复原状
        setTimeout(() => {
          player.width = originalSize.width;
          player.height = originalSize.height;
          player.damageMultiplier = 1;
          player.jumpForceMultiplier = 1;
          
          // 恢复时的粒子效果
          this.game.particleSystem.createParticles(
            player.x,
            player.y,
            '#FFFFFF',
            15,
            4,
            5,
            30
          );
        }, duration * (1000/60));
        break;
        
      case 'speedBoost':
        // 速度提升效果
        const originalSpeed = player.maxSpeed;
        const originalJumpCooldown = player.jumpCooldown;
        
        // 提升速度和跳跃频率
        player.maxSpeed *= config.effects.speedMultiplier;
        player.jumpCooldown *= config.effects.jumpCooldown;
        
        // 添加速度特效
        player.speedTrail = true;
        
        // 定时恢复
        setTimeout(() => {
          player.maxSpeed = originalSpeed;
          player.jumpCooldown = originalJumpCooldown;
          player.speedTrail = false;
        }, duration * (1000/60));
        break;
        
      case 'doubleJump':
        player.maxJumps = 3;
        this.addStatusEffect('doubleJump', config.duration);
        break;
        
      case 'shield':
        player.hasShield = true;
        this.addStatusEffect('shield', config.duration);
        break;
        
      case 'magnet':
        player.hasMagnet = true;
        player.magnetRange = config.range;
        this.addStatusEffect('magnet', config.duration);
        break;
        
      case 'timeSlow':
        Settings.platform.speed *= config.slowFactor;
        this.addStatusEffect('timeSlow', config.duration);
        break;
    }
    
    // 显示获得道具提示
    this.showEffectNotification();
    
    // 播放音效
    // this.game.audio.play('powerup');
  }
  
  addStatusEffect(type, duration) {
    // 创建状态效果
    const effect = {
      type,
      duration,
      originalValue: null
    };
    
    // 保存原始值（用于恢复）
    switch(type) {
      case 'speedBoost':
        effect.originalValue = this.game.player.maxSpeed;
        break;
      case 'timeSlow':
        effect.originalValue = Settings.platform.speed;
        break;
    }
    
    // 添加到状态效果列表
    this.game.player.statusEffects = this.game.player.statusEffects || [];
    this.game.player.statusEffects.push(effect);
    
    // 设置定时器移除效果
    setTimeout(() => {
      this.removeStatusEffect(effect);
    }, duration * (1000/60));
  }
  
  removeStatusEffect(effect) {
    // 恢复原始值
    switch(effect.type) {
      case 'doubleJump':
        this.game.player.maxJumps = Settings.player.maxJumps;
        break;
      case 'speedBoost':
        this.game.player.maxSpeed = effect.originalValue;
        break;
      case 'shield':
        this.game.player.hasShield = false;
        break;
      case 'magnet':
        this.game.player.hasMagnet = false;
        break;
      case 'timeSlow':
        Settings.platform.speed = effect.originalValue;
        break;
    }
    
    // 从状态列表中移除
    const index = this.game.player.statusEffects.indexOf(effect);
    if (index > -1) {
      this.game.player.statusEffects.splice(index, 1);
    }
  }
  
  showEffectNotification() {
    // 创建浮动文字效果
    const text = {
      content: `获得 ${this.type}!`,
      x: this.x,
      y: this.y - 30,
      color: this.color,
      alpha: 1,
      velocity: -2
    };
    
    // 添加到游戏的浮动文字列表
    this.game.floatingTexts = this.game.floatingTexts || [];
    this.game.floatingTexts.push(text);
  }
  
  checkCollision(player) {
    return (
      player.x - player.width/2 < this.x + this.width/2 &&
      player.x + player.width/2 > this.x - this.width/2 &&
      player.y - player.height/2 < this.y + this.height/2 &&
      player.y + player.height/2 > this.y - this.height/2
    );
  }
} 